import axios from "axios";
import store from "@/store";
import { Message } from "element-ui";
let apiBaseUrl = "http://localhost:8888";

// 创建 axios 实例
const service = axios.create({
  baseURL: "/api",
  timeout: 60000,
  withCredentials: true, //跨域
});

// // 配置请求拦截器
service.interceptors.request.use(
  (config) => {
    const token = store.state.token;
    if (token) {
      // 如果存在token，让每次请求携带token
      config.headers["Oauth-Token"] = token;
    }
    return config;
  },
  (error) => {
    console.log(error);
    return Promise.reject(error);
  }
);

// respone拦截器
service.interceptors.response.use(
  (response) => {
    //全局统一处理 Session超时

    return response.data;
  },
  (error) => {
    Message({
      type: "warning",
      showClose: true,
      message: "连接超时",
    });
    return Promise.reject("error");
  }
);
export default service;
